from flask_migrate import Migrate, Manager, MigrateCommand
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import func

db = SQLAlchemy()


# 基础CRUD操作类
class CRUD:

    def add(self, resource):
        db.session.add(resource)
        return db.session.commit()

    def update(self):
        return db.session.commit()

    def delete(self, resource):
        db.session.delete(resource)
        return db.session.commit()


# 基础实体类
class Base(db.Model, CRUD):
    __abstract__ = True

    id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True)
    create_user = db.Column(db.String, nullable=False)
    create_time = db.Column(db.DateTime, nullable=False, default=func.now())  # 记录的创建时间
    update_user = db.Column(db.String, nullable=False)
    update_time = db.Column(db.DateTime, nullable=False, default=func.now())  # 记录的更新时间

    def __init__(self, create_user, update_user):
        self.create_user = create_user
        self.update_user = update_user
